package com.tobeface.sns.interfaces.admin;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import nl.captcha.Captcha;
import nl.captcha.servlet.CaptchaServletUtil;
import nl.captcha.text.renderer.DefaultWordRenderer;
import nl.captcha.text.renderer.WordRenderer;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.ImmutableList;
import com.kissme.core.web.Webs;
import com.tobeface.sns.interfaces.util.HttpCaptchaHelper;
import com.tobeface.sns.interfaces.util.JsonMessage;

/**
 * 
 * @author loudyn
 * 
 */
@Controller
public class CaptchaController {

	private static final List<Font> SMALL_FONTS = ImmutableList.of(
																	new Font("Courier New", Font.ITALIC, 16),
																	new Font("Arial", Font.ITALIC, 16),
																	new Font("Times New Roman", Font.ITALIC, 16),
																	new Font("Verdana", Font.ITALIC, 16)
															);

	private static final List<Font> MEDIUM_FONTS = ImmutableList.of(
																	new Font("Courier New", Font.ITALIC, 24),
																	new Font("Arial", Font.ITALIC, 24),
																	new Font("Times New Roman", Font.ITALIC, 24),
																	new Font("Verdana", Font.ITALIC, 24)
															);

	private static final List<Font> FONTS = ImmutableList.of(
																new Font("Courier New", Font.ITALIC, 32),
																new Font("Arial", Font.ITALIC, 32),
																new Font("Times New Roman", Font.ITALIC, 32),
																new Font("Verdana", Font.ITALIC, 32)
													);

	private static final List<Color> COLORS = ImmutableList.of(new Color(16, 170, 221));
	private static final WordRenderer SMALL_RENDERER = new DefaultWordRenderer(COLORS, SMALL_FONTS);
	private static final WordRenderer MEDIUM_RENDERER = new DefaultWordRenderer(COLORS, MEDIUM_FONTS);
	private static final WordRenderer RENDERER = new DefaultWordRenderer(COLORS, FONTS);

	/**
	 * 
	 * @param session
	 * @param out
	 * @throws IOException 
	 */
	@RequestMapping(value = "/captcha/small", method = RequestMethod.GET)
	public void smallCaptcha(HttpSession session, HttpServletResponse response) throws IOException {
		Captcha captcha = new Captcha.Builder(64, 16).addText(SMALL_RENDERER).gimp().build();
		writeCaptcha(captcha, response, session);
	}

	private void writeCaptcha(Captcha captcha, HttpServletResponse response, HttpSession session) throws IOException {
		response.setContentType("image/jpeg");
		CaptchaServletUtil.writeImage(response.getOutputStream(), captcha.getImage());
		HttpCaptchaHelper.storeCaptcha(captcha, session);
	}

	/**
	 * 
	 * @param session
	 * @param out
	 * @throws IOException 
	 */
	@RequestMapping(value = "/captcha/medium", method = RequestMethod.GET)
	public void mediumCaptcha(HttpSession session, HttpServletResponse response) throws IOException {
		Captcha captcha = new Captcha.Builder(96, 24).addText(MEDIUM_RENDERER).gimp().build();
		writeCaptcha(captcha, response, session);
	}

	/**
	 * 
	 * @param session
	 * @param out
	 * @throws IOException 
	 */
	@RequestMapping(value = "/captcha", method = RequestMethod.GET)
	public void captcha(HttpSession session, HttpServletResponse response) throws IOException {
		Captcha captcha = new Captcha.Builder(128, 32).addText(RENDERER).gimp().build();
		writeCaptcha(captcha, response, session);
	}

	/**
	 * 
	 * @param requestedWith
	 * @param captcha
	 * @param session
	 * @return
	 */
	@RequestMapping(value = "/captcha/validate", method = RequestMethod.POST)
	@ResponseBody
	public JsonMessage isCaptchaCorrect(@RequestHeader(value = "X-Requested-With", required = false) String requestedWith, @RequestParam("captcha") String captcha,
										HttpSession session) {

		// use on ajax request,if not ,return as soon as possible
		if (!Webs.isAjax(requestedWith)) {
			return JsonMessage.one().notOk();
		}

		try {

			HttpCaptchaHelper.checkCaptcha(captcha, session);
			return JsonMessage.one().ok();
		} catch (Exception e) {
			return JsonMessage.one().notOk();
		}
	}
}
